package com.rabbitmqproduce.controller;

import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

@RestController
public class SendMsgController {
	@Resource
	RabbitTemplate rabbitTemplate;
	@GetMapping("/sendMsg")
	String sendMsg(){
		for (int i = 0; i <= 200; i++) {
			final int userId=1;
			new Thread(new Runnable() {
				@Override
				public void run() {
					String messageId = UUID.randomUUID().toString();
					String messageData = userId + "";
					String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
					Map<String, Object> map=new HashMap<>();
					map.put("messageId",messageId);
					map.put("messageData",messageData);
					map.put("createTime",createTime);
					rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
					rabbitTemplate.convertAndSend("MiaoShaExchange","MiaoShaRouting",map);
				}
			}).start();
		}
		return "消息发送成功";
	}
}
